package com.huluobo.a2205parkdemo.department

import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2205parkdemo.R
import com.huluobo.a2205parkdemo.adapter.DepartmentAdapter
import com.huluobo.a2205parkdemo.databinding.ActivityDepartmentBinding
import com.huluobo.base.bean.DepartmentBean
import com.huluobo.base.bean.DepartmentBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.parse
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class DepartmentActivity : BaseActivity<ActivityDepartmentBinding, DepartmentViewModel>() {
    private lateinit var departmentAdapter: DepartmentAdapter
    private var max = 0
    private lateinit var departmentBeanItem: DepartmentBeanItem
    override fun initData() {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_USER_ID] = SPUtils.getInstance().getInt(Const.PARAM_U_ID)
        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        vm.getAllDepartment(body)

        vm.getAllDepartmentSuccess.observe(this) {
            val list = Gson().fromJson(it, DepartmentBean::class.java)
            //拿到部门集合中useId的最大值,因为上传的时候需要传递一个userId,而且不能重复
            max = list.maxOf {
                it.departmentUserId
            }
            departmentAdapter.data.clear()
            departmentAdapter.data.addAll(list)
            departmentAdapter.notifyDataSetChanged()
        }

        vm.addDepartmentSuccess.observe(this) {
            ToastUtils.showLong("添加成功")
            vm.getAllDepartment(body)
        }

        vm.deleteDepartmentSuccess.observe(this) {
            ToastUtils.showLong("删除成功")
            departmentAdapter.remove(departmentBeanItem)
        }
    }

    override fun initView() {
        binding.departmentHeader.headerTitle.text = "人资管理"
        binding.departmentHeader.headerBack.visibility = View.VISIBLE
        binding.departmentHeader.headerBack.setOnClickListener { finish() }
        binding.departmentHeader.headerAdd.visibility = View.VISIBLE
        binding.departmentHeader.headerAdd.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.dialog_add_department, null)
            AlertDialog.Builder(this)
                .setView(view)
                .setTitle("添加部门")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", object : OnClickListener {
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        val departmentName = view.findViewById<EditText>(R.id.add_department_title_et).text.toString()
                        if (departmentName.isNotEmpty()) {
                            //添加部门方法
                            val map = mutableMapOf<String, Any>()
                            map[Const.PARAM_DEPARTMENT_NAME] = departmentName
                            map[Const.PARAM_DEPARTMENT_USER_ID] = max + 1

                            val json = Gson().toJson(map)
                            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                            vm.addDepartment(body)
                        }
                    }
                })
                .show()
        }

        binding.departmentRv.layoutManager = GridLayoutManager(this, 2)
        departmentAdapter = DepartmentAdapter()
        binding.departmentRv.adapter = departmentAdapter

        departmentAdapter.addChildClickViewIds(R.id.item_department_delete_btn)
        departmentAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.item_department_delete_btn) {
                departmentBeanItem = departmentAdapter.data[position]
                val departmentId = departmentAdapter.data[position].departmentId
                val departmentUserId = departmentAdapter.data[position].departmentUserId

                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_DEPARTMENT_ID] = departmentId
                map[Const.PARAM_DEPARTMENT_USER_ID] = departmentUserId
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.deleteDepartment(body)
            }
        }
    }
}